package org.jash.ting13.user

import android.graphics.drawable.BitmapDrawable
import android.widget.Toast
import org.jash.ting13.databinding.ActivityLoginBinding
import org.jash.utils.log
import org.jash.utils.mvi.BaseActivity

class LoginActivity : BaseActivity<ActivityLoginBinding, LoginViewModel>() {
    val user = LoginIntent.User()
    override fun initData() {
        viewModel.state.observe(this) {
            when (it) {
                is LoginState.Verify -> binding.verify.endIconDrawable = BitmapDrawable(resources, it.img).apply {  }
                is LoginState.Success -> log(it.message)
                is LoginState.Failure -> {
                    Toast.makeText(this, it.message, Toast.LENGTH_SHORT).show()
                    viewModel.intent.trySend(LoginIntent.Verify)
                }
            }
        }
        viewModel.intent.trySend(LoginIntent.Verify)
    }

    override fun initView() {
        binding.user = user
        binding.verify.setEndIconOnClickListener { viewModel.intent.trySend(LoginIntent.Verify) }
        binding.login.setOnClickListener { viewModel.intent.trySend(user.copy()) }
    }

}